<!DOCTYPE html>
<html>
<head>
    <title>访问记录</title>
    {% include 'system/common/header.html' %}
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <table id="access-dataTable" lay-filter="access-dataTable"></table>
    </div>
</div>
<script type="text/html" id="access-toolbar">
    {% if authorize("system:access:remove") %}
        <button class="pear-btn pear-btn-sm" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>
            批量删除
        </button>
    {% endif %}
</script>


{# 修改操作 #}
<script type="text/html" id="access-bar">
    {% if authorize("system:access:remove") %}
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
                class="pear-icon pear-icon-ashbin"> 删除</i>
        </button>
    {% endif %}
</script>


{% include 'system/common/footer.html' %}
<script>
  layui.use(['table', 'form', 'jquery'], function () {
    let table = layui.table
    let form = layui.form
    let $ = layui.jquery
    // let MODULE_PATH = '/admin/system/access/'
    let MODULE_PATH = "{{ url_for('system.access.main') }}"
    let cols = [
      [
        {
          type: 'checkbox'
        },
        {
          field: 'id',
          title: '记录ID',
          unresize: true,
          align: 'center',
          unresize: true,
          width: 80
        },
        {
          field: 'user_id',
          title: '用户ID',
          unresize: true,
          align: 'center',
        },
        {
          field: 'ip',
          title: '访问IP',
          unresize: true,
          align: 'center',
          type: 'password'
        },
        {
          field:'flowtotal',
          title:'访问流量',
          unresize: true,
          align: 'center'
        },
        {
          field:'city',
          title:'访问城市',
          unresize: true,
          align: 'center'
        },
        {
          field:'access_time',
          title:'访问时间',
          unresize: true,
          align: 'center'
        },
        {
          field: 'url',
          title: '访问URL',
          unresize: true,
          align: 'center'
        },
        {
          field: 'referer',
          title: '访问来源',
          unresize: true,
          align: 'center'
        },
        {
          field: 'userAgent',
          title: '访问设备',
          unresize: true,
          align: 'center'
        },
        {
          field: 'isp',
          title: '运营商',
          unresize: true,
          align: 'center'
        },
        {
          field:'position',
          title:'位置',
          unresize: true,
          align: 'center'
        },
        {
          title: '操作',
          toolbar: '#access-bar',
          align: 'center',
          unresize: true,
          width: 200
        }
      ]
    ]

    table.render({
      elem: '#access-dataTable',
      url: MODULE_PATH + 'table',
      page: true,
      cols: cols,
      skin: 'line',
      toolbar: '#access-toolbar',
      defaultToolbar: [{
        layEvent: 'refresh',
        icon: 'layui-icon-refresh',
      }, 'filter', 'print', 'exports']
    })

    table.on('tool(access-dataTable)', function (obj) {
      if (obj.event === 'remove') {
        window.remove(obj)
      }
    })

    table.on('toolbar(access-dataTable)', function (obj) {
      if (obj.event === 'add') {
        window.add()
      } else if (obj.event === 'refresh') {
        window.refresh()
      } else if (obj.event === 'batchRemove') {
        window.batchRemove(obj)
      }
    })

    //弹出窗设置 自己设置弹出百分比
    function screen () {
      if (typeof width !== 'number' || width === 0) {
        width = $(window).width() * 0.5
      }
      if (typeof height !== 'number' || height === 0) {
        height = $(window).height() - 100
      }
      return [width + 'px', height + 'px']
    }


    window.remove = function (obj) {
      layer.confirm('确定要删除该参数吗？', {
        icon: 3,
        title: '提示'
      }, function (index) {
        layer.close(index)
        let loading = layer.load()
        $.ajax({
          url: MODULE_PATH + 'remove',
          data: { 
            id: obj.data['id'],
            filename: obj.data['name']
          },
          dataType: 'json',
          type: 'POST',
          success: function (res) {
            layer.close(loading)
            if (res.success) {
              layer.msg(res.msg, {
                icon: 1,
                time: 1000
              }, function () {
                obj.del()
              })
            } else {
              layer.msg(res.msg, {
                icon: 2,
                time: 1000
              })
            }
          }
        })
        // console.log(obj.data['filename']);
      })
    }

    window.batchRemove = function (obj) {
      let data = table.checkStatus(obj.config.id).data
      if (data.length === 0) {
        layer.msg('未选中数据', {
          icon: 3,
          time: 1000
        })
        return false
      }
      var ids = []
      var hasCheck = table.checkStatus('access-dataTable')
      var hasCheckData = hasCheck.data
      if (hasCheckData.length > 0) {
        $.each(hasCheckData, function (index, element) {
          ids.push(element.id)
        })
      }
      layer.confirm('确定要删除这些参数吗？', {
        icon: 3,
        title: '提示'
      }, function (index) {
        layer.close(index)
        let loading = layer.load()
        $.ajax({
          url: MODULE_PATH + 'batchRemove',
          data: { ids: ids },
          dataType: 'json',
          type: 'POST',
          success: function (res) {
            layer.close(loading)
            if (res.success) {
              layer.msg(res.msg, {
                icon: 1,
                time: 1000
              }, function () {
                table.reload('access-dataTable')
              })
            } else {
              layer.msg(res.msg, {
                icon: 2,
                time: 1000
              })
            }
          }
        })
      })
    }

    window.refresh = function () {
      table.reload('access-dataTable')
    }
   

  })
</script>